<template>
  <el-row :gutter="10">
    <el-col>
      <el-card class="layout-query">
        <el-form ref="queryRef" :inline="true">
          <el-form-item label="请选择公众号">
            <el-select v-model="appid" placeholder="请选择公众号" size="default" style="width: 240px" @change="change">
              <el-option v-for="item in data" :label="item.name" :value="item.appid" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <div class="app-container">
          <div class="wechat-reply-wrapper wechat-menu">
            <div class="ibox-content clearfix">
              <el-row>
                <el-col :span="8">
                  <div class="view-wrapper col-sm-4">
                    <div class="mobile-header">公众号</div>
                    <section class="view-body">
                      <div class="time-wrapper"><span class="time">9:36</span></div>
                    </section>
                    <div class="menu-footer">
                      <ul class="flex">
                        <li v-for="(menu, index) in menus" :class="{ active: menu === checkedMenu }">
                          <span @click="activeMenu(menu, index, null)"
                            ><i class="icon-sub" />{{ menu.name || '一级菜单' }}</span
                          >
                          <div class="sub-menu">
                            <ul>
                              <li v-for="(child, cindex) in menu.subButtons" :class="{ active: child === checkedMenu }">
                                <span @click="activeMenu(child, cindex, index)">{{ child.name || '二级菜单' }}</span>
                              </li>
                              <li v-if="menu.subButtons.length < 5" @click="addChild(menu, index)">
                                <i class="icon-add" />
                              </li>
                            </ul>
                          </div>
                        </li>
                        <li v-if="menus.length < 3" @click="addMenu()"><i class="icon-add" /></li>
                      </ul>
                    </div>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div v-show="checkedMenuId !== null" class="control-wrapper menu-control col-sm-8">
                    <section>
                      <div class="control-main">
                        <h3 class="popover-title">
                          菜单名称 <a class="fr" href="javascript:void(0);" @click="delMenu">删除</a>
                        </h3>
                        <p class="tips-txt">已添加子菜单，仅可设置菜单名称。</p>
                        <div class="menu-content control-body">
                          <form action="">
                            <div class="form-group clearfix">
                              <label for="" class="el-form-item__label" style="width: 80px">菜单名称</label>
                              <div class="col-sm-9 group-item">
                                <input
                                  v-model="checkedMenu.name"
                                  type="text"
                                  placeholder="菜单名称"
                                  style="width: 370px"
                                  class="form-control" />
                              </div>
                            </div>
                            <div class="form-group clearfix">
                              <label class="el-form-item__label" style="width: 80px" for="">规则状态</label>
                              <div class="group-item col-sm-9">
                                <el-select
                                  v-model="checkedMenu.type"
                                  placeholder="Select"
                                  size="default"
                                  style="width: 240px">
                                  <el-option label="关键字" value="click">关键字</el-option>
                                  <el-option label="跳转网页" value="view">跳转网页</el-option>
                                  <el-option label="小程序" value="miniprogram">小程序</el-option>
                                </el-select>
                              </div>
                            </div>
                            <div class="menu-control-box">
                              <!-- 关键字 -->
                              <div class="keywords item" :class="{ show: checkedMenu.type == 'click' }">
                                <span class="el-form-item__label">关键字</span>
                                <input
                                  v-model="checkedMenu.key"
                                  type="text"
                                  placeholder="请输入关键字"
                                  class="form-control" />
                              </div>
                              <!-- 跳转地址 -->
                              <div class="url item" :class="{ show: checkedMenu.type == 'view' }">
                                <span class="el-form-item__label">跳转地址</span>
                                <input
                                  v-model="checkedMenu.url"
                                  type="text"
                                  placeholder="请输入跳转地址"
                                  class="form-control" />
                                <p class="text-left" />
                              </div>
                              <!-- 小程序 -->
                              <div class="wrchat-app item" :class="{ show: checkedMenu.type == 'miniprogram' }">
                                <div class="list">
                                  <span class="el-form-item__label">appId</span>
                                  <input v-model="checkedMenu.appId" class="form-control" type="text" />
                                </div>
                                <div class="list">
                                  <span class="el-form-item__label">备用网页url</span>
                                  <input v-model="checkedMenu.url" class="form-control" type="text" />
                                </div>
                                <div class="list">
                                  <span class="el-form-item__label">小程序路径</span>
                                  <input v-model="checkedMenu.pagePath" class="form-control" type="text" />
                                </div>
                              </div>
                              <!-- 多客服 -->
                              <div class="service item">
                                <p>回复内容</p>
                                <textarea cols="60" rows="10" />
                              </div>
                            </div>
                          </form>
                        </div>
                      </div>
                    </section>
                  </div>
                </el-col>
              </el-row>
            </div>
            <div class="ibox-content submit">
              <button class="el-button el-button--primary" @click="submit">保存发布</button>
            </div>
          </div>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { getMenusApi, setMenusApi } from '@/api/wx/mp'
import { getWxListApi } from '@/api/wx/account'

const appid = ref()
const data = ref([])
const menus = ref([])
const checkedMenuId = ref(null)
const parentMenuId = ref(null)
const checkedMenu = ref({
  type: 'click',
  name: ''
})
const defaultChildData = () => {
  return {
    type: 'click',
    name: ''
  }
}
const defaultMenusData = () => {
  return {
    type: 'click',
    name: '',
    subButtons: []
  }
}

//获取微信列表
getWxList()
function getWxList() {
  getWxListApi().then((res) => {
    data.value = res.data.filter((res) => {
      return res.type === 'mp'
    })
    appid.value = data.value[0].appid
    change()
  })
}

function change() {
  getMenus()
}


function getMenus() {
  // console.log(123)
  getMenusApi(appid.value).then((res: any) => {
    menus.value = res.data.menu.buttons
    // console.log(menus.value)
  })
}
function check() {
  if (checkedMenuId.value === null) {
    return true
  }
  if (checkedMenu.value.name === '') {
    // $eb.message('请输入按钮名称!');
    ElMessage({
      message: '请输入按钮名称',
      type: 'error',
      duration: 1000,
      onClose: () => {
        // this.init()
      }
    })
    return false
  }
  if (checkedMenu.value.type === 'click' && !checkedMenu.value.key) {
    // $eb.message('请输入关键字!');
    ElMessage({
      message: '请输入关键字',
      type: 'error',
      duration: 1000,
      onClose: () => {
        // this.init()
      }
    })
    return false
  }
  if (checkedMenu.value.type === 'view' && !checkedMenu.value.url) {
    ElMessage({
      message: '请输入跳转地址',
      type: 'error',
      duration: 1000,
      onClose: () => {
        // this.init()
      }
    })

    return false
  }
  if (
    checkedMenu.value.type === 'miniprogram' &&
    (!checkedMenu.value.appId || !checkedMenu.value.pagePath )
  ) {
    // $eb.message('请填写完整小程序配置!');
    ElMessage({
      message: '请填写完整小程序配置',
      type: 'error',
      duration: 1000,
      onClose: () => {
        // this.init()
      }
    })
    return false
  }
  return true
}

function activeMenu(menu, index, pid) {
  if (!check()) {
    return false
  }
  pid === null
    ? (checkedMenu.value = menu)
    : ((checkedMenu.value = menus.value[pid].subButtons[index]), (parentMenuId.value = pid))
  checkedMenuId.value = index
}
function addChild(menu, index) {
  if (!check()) {
    return false
  }
  var data = defaultChildData()
  var id = menu.subButtons.length
  menu.subButtons.push(data)
  checkedMenu.value = data
  checkedMenuId.value = id
  parentMenuId.value = index
}
function addMenu() {
  if (!check()) {
    return false
  }
  var data = defaultMenusData()
  var id = menus.value.length
  menus.value.push(data)
  checkedMenu.value = data
  checkedMenuId.value = id
  parentMenuId.value = null
}
function delMenu() {
  console.log(parentMenuId.value)
  parentMenuId.value === null
    ? menus.value.splice(checkedMenuId.value, 1)
    : menus.value[parentMenuId.value].subButtons.splice(checkedMenuId.value, 1)
  parentMenuId.value = null
  checkedMenu.value = {}
  checkedMenuId.value = null
}
function add() {
  setMenusApi(appid.value,{ buttons: menus.value }).then((res) => {
    ElMessage({
      message: '添加完成',
      type: 'success',
      duration: 1000
    })
  })
}
function submit() {
  if (!check()) {
    return false
  }
  // console.log(this.menus.length)
  if (!menus.value.length) {
    ElMessage({
      message: '请添加菜单',
      type: 'error',
      duration: 1000
    })
    return false
  }
  // console.log(menus.value)
  add()
}
</script>

<style rel="stylesheet/scss" scoped>
body {
  font-family: 'open sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;
}

.form-group {
  font-size: 12px;
}

.form-control {
  background-color: #fff;
  background-image: none;
  border: 1px solid #e5e6e7;
  border-radius: 1px;
  color: inherit;
  display: block;
  padding: 6px 12px;
  -webkit-transition:
    border-color 0.15s ease-in-out 0s,
    box-shadow 0.15s ease-in-out 0s;
  transition:
    border-color 0.15s ease-in-out 0s,
    box-shadow 0.15s ease-in-out 0s;
  width: 100%;
  font-size: 14px;
}

.flex {
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;
}

#app .layout-ceiling-main a {
  color: #9ba7b5;
}

#table-list .mp-header-wrapper h1 {
  padding-left: 15px;
  float: left;
  font-size: 18px;
  line-height: 24px;
  padding-bottom: 20px;
  font-weight: 400;
  color: #464c5b;
}

.mp-form .ivu-tree li {
  margin: 0;
}

.mp-form .demo-upload-list img {
  width: 100%;
  height: 100%;
}

.mp-form .demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}

.wechat-reply-wrapper .fr {
  float: right;
  margin-left: 100px;
}

.wechat-reply-wrapper .clearfix:after {
  content: '.';
  display: block;
  height: 0;
  visibility: hidden;
  clear: both;
}

.wechat-reply-wrapper .ibox-title p {
  border-left: 2px solid #2494f2;
  text-indent: 8px;
}

.wechat-reply-wrapper .ibox-content {
  background-color: #ffffff;
  padding: 15px;
  font-size: 12 ox;
}

.wechat-reply-wrapper .ibox-content .view-wrapper {
  position: relative;
  width: 317px;
  background-image: url(../../../assets/wechat/mobile_head.png);
  background-repeat: no-repeat;
  background-position: left top;
  background-color: #f5f5f5;
}

.wechat-reply-wrapper .ibox-content .view-wrapper .mobile-header {
  position: relative;
  left: 0;
  top: 36px;
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 16px;
}

.wechat-reply-wrapper .ibox-content .view-wrapper .view-body {
  margin-top: 65px;
  background-color: #f5f5f5;
  height: 500px;
}

.wechat-reply-wrapper .view-wrapper .view-body .time-wrapper {
  margin-bottom: 10px;
  text-align: center;
}

.wechat-reply-wrapper .view-wrapper .view-body .time-wrapper .time {
  display: inline-block;
  color: #f5f5f5;
  display: inline-block;
  color: #f5f5f5;
  background: rgba(0, 0, 0, 0.3);
  padding: 3px 8px;
  border-radius: 3px;
  font-size: 12px;
}

.wechat-reply-wrapper .view-wrapper .view-body .view-item .avatar img {
  max-width: 100%;
  height: auto;
}

.wechat-reply-wrapper .submit {
  text-align: center;
}

/* 图文 */

/* 音乐 */

.view-wrapper .view-body .view-item.music-box .box-content p {
  width: 75%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* 右侧控制器 */
.wechat-reply-wrapper .ibox-content .control-wrapper {
  position: relative;
  width: 535px;
  height: 565px;
  padding: 0;
  margin-left: 20px;
  border: 1px solid #e2e2e2;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body {
  margin-top: 40px;
  padding: 0 10px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .form-group label {
  font-weight: normal;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .form-group .tips:after {
  content: '*';
  color: red;
  position: absolute;
  margin-left: 4px;
  font-weight: bold;
  line-height: 1.8em;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .form-group .group-item {
  position: relative;
  margin-left: 20px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .form-group .group-item .file-btn {
  position: absolute;
  right: 15px;
  top: 0;
  display: block;
  width: 66px;
  border-radius: 6px;
  cursor: pointer;
  padding: 0.5rem;
  background-color: #18a689;
  color: #fff;
  text-align: center;
  height: 100%;
  line-height: 23px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .form-group .group-item textarea {
  resize: none;
  width: 100%;
  height: 100px;
  padding: 10px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .tips-info {
  padding-left: 100px;
  font-size: 12px;
  color: #737373;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .control-main .control-item {
  display: none;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body .control-main .control-item.show {
  display: block;
}

/* 微信菜单定制 */
.wechat-menu {
  position: relative;
}

.wechat-menu ul {
  padding: 0;
}

.wechat-menu .menu-footer {
  padding-left: 43px;
  background: url('../../../assets/wechat/mobile_foot.png') no-repeat 0px 0px;
  border-top: 1px solid #e7e7eb;
}

.wechat-menu .menu-footer span {
  display: block;
  font-size: 12px;
}

.wechat-menu .menu-footer .icon-add {
  background: url(../../../assets/wechat/index.png) 0 0 no-repeat;
  width: 14px;
  height: 14px;
  vertical-align: middle;
  display: inline-block;
  margin-top: -2px;
  border-bottom: none !important;
}

.wechat-menu .menu-footer li {
  position: relative;
  -webkit-flex: 1;
  -moz-flex: 1;
  -ms-flex: 1;
  flex: 1;
  height: 50px;
  line-height: 50px;
  text-align: center;
  cursor: pointer;
  list-style: none;
  border: 1px solid transparent;
  border-right: 1px solid #e7e7eb;
}

.wechat-menu .menu-footer .icon-sub {
  background: url(../../../assets/wechat/index.png) 0 -48px no-repeat;
  width: 7px;
  height: 7px;
  vertical-align: middle;
  display: inline-block;
  margin-right: 2px;
  margin-top: -2px;
}

.wechat-menu .menu-footer .sub-menu {
  position: absolute;
  border-radius: 3px;
  border: 1px solid #d0d0d0;
  display: block;
  bottom: 60px;
  width: 100%;
  background-color: #fafafa;
}

.wechat-menu .menu-footer .sub-menu:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  background: #fafafa;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  bottom: -5px;
  border-bottom: 1px solid #d0d0d0;
  border-right: 1px solid #d0d0d0;
  left: 50%;
  margin-left: -5px;
}

.wechat-menu .menu-footer .sub-menu li {
  border-right: 0;
  border-bottom: 1px solid #d0d0d0;
}

.wechat-menu .menu-footer .sub-menu li:last-child {
  border-bottom: 0;
}

.wechat-menu .menu-footer .active {
  border: 1px solid #44b549;
}

.wechat-menu .menu-footer .sub-menu li.active {
  border: 1px solid #44b549 !important;
}

/* 右侧 */
.wechat-menu .menu-control .popover-title {
  padding: 8px 14px;
  margin: 0;
  font-size: 14px;
  background-color: #f7f7f7;
  border-bottom: 1px solid #ebebeb;
  border-radius: 5px 5px 0 0;
  font-weight: 400;
}

.wechat-menu .menu-control .popover-title a {
  color: #06c;
  font-size: 12px;
}

.wechat-menu .menu-control .tips-txt {
  line-height: 40px;
  padding: 0 20px;
  font-size: 12px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body.menu-content {
  padding: 0 20px;
  margin-top: 0;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body.menu-content .radio {
  display: inline-block !important;
  width: 45%;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body.menu-content .menu-control-box {
  padding: 0 20px;
}

.wechat-reply-wrapper .ibox-content .control-wrapper .control-body.menu-content .menu-control-box .radio {
  display: block !important;
  width: 100%;
}

.menu-control-box .item {
  display: none;
}

.menu-control-box .show {
  display: block;
}
</style>
